import React, { Component } from 'react'

export default class Item extends Component {
  //勾选 取消
  handleChecked = (id) => {
    //高阶函数的写法
    return (e) => {
      this.props.updateTodo(id, e.target.checked)
    }
  }
  //删除一个todo
  handleDelete = (id) => {
    if (window.confirm('确定删除吗')) {
      this.props.deleteTodo(id)
    }
  }
  render() {
    //获取list从app中得到的数据 并且渲染item
    const { id, name, done } = this.props
    return (
      <li>
        <label>
          <input
            type="checkbox"
            checked={done}
            onChange={this.handleChecked(id)}
          />
          <span>{name}</span>
        </label>
        <button
          //点击删除 获取当前项的id
          onClick={() => {
            this.handleDelete(id)
          }}
          className="btn btn-danger">
          删除
        </button>
      </li>
    )
  }
}
